package edu.szu.huhanpeng.doodlingFight.controller.showController.manageDone 
{
	import com.greensock.TweenLite;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.view.components.ChessComponent;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import org.puremvc.as3.interfaces.IAsyncCommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.AsyncCommand;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class WrRrFillEmptySquareSc extends AsyncCommand implements INotifier, IAsyncCommand 
	{
		
		//通知名	:GlobalConst.noteName.SHOW_FILL_SQUARE
		//通知类型	:GlobalConst.noteType.CALLBACK
		//包体类型	:Array
		//包体信息	:arr[0]为Vector.<IChessData>，元素为需要更新显示位置的棋子data，顺序为左起从下到上  arr[1]为Vector.<IChessData>，元素为舞台外等待加入棋盘的棋子data，顺序为左起从下到上		arr[2]为回调函数  
		//发送方	:SrHrFillEmptySquareDc
		
		private static const DURATION:Number = 1;	//移动持续时间，单位为秒
		private static const DELAY:Number = 1000;	//等待反应时间，单位为毫秒
		private var _callback:Function;
		
		private var _needUpdate:Vector.<IChessData>;
		private var _waiting:Vector.<IChessData>;
		
		override public function execute(note:INotification):void 
		{
			var body:Array = note.getBody() as Array;
			_needUpdate = body[0] as Vector.<IChessData>;
			_waiting = body[1] as Vector.<IChessData>;
			body.splice( 0, 2 );
			
			if ( _needUpdate == null || _waiting == null ) 
			{
				if (GlobalConst.setting.DEBUG) 
				{
					throw new Error("body类型出错");
				}
				return;
			}
			
			var chess:IChessData;
			var chessComponent:ChessComponent;
			
			if ( _needUpdate.length != 0 ) 
			{
				for (var i:int = 0; i < _needUpdate.length; i++) 
				{
					chess = _needUpdate[i];
					chessComponent = facade.retrieveMediator( chess.name ).getViewComponent() as ChessComponent;
					TweenLite.to( chessComponent, DURATION, { x:chess.x, y:chess.y, onComplete:moveComplete } );
				}
			}
			else
			{
				moveComplete();
			}

		}
		
		private function moveComplete():void 
		{
			var chess:IChessData;
			var chessComponent:ChessComponent;
			
			for (var j:int = 0; j < _waiting.length; j++) 
			{
				chess = _waiting[j];
				chessComponent = facade.retrieveMediator( chess.name ).getViewComponent() as ChessComponent;
				chessComponent.alpha = 0;
				//sendNotification( GlobalConst.noteName.STAGE_ADD_CHILD, _waiting[j].name );
				sendNotification( GlobalConst.noteName.ADD_CHESS, chessComponent );
				TweenLite.to( chessComponent, DURATION, {x:chess.x, y:chess.y, alpha:1, onComplete:commandComplete} );
			}
		}		
		
	}

}